#!/usr/bin/env python2
#coding:utf-8
""" Author:   Rusty Stiles
    Purpose: Module designed to make input files for another program(SandF.f),
               reads data from config file and writes data to supplied filemanes
    Created: 06/08/2011
"""


from configobj import ConfigObj
from random import randint


def writeinputs(config, filename):
    """Writes the Parameters in the dictionary, config, to the specified 
       filename, also will generate random number for config
       section 'random' where random has a 'low' and 'high' item"""

    try:
        low = int(config['random']['low'])
        high = int(config['random']['high'])
        random = randint(low, high)
        config['random']['val'] = str(random)
    except KeyError:
        pass

    with open(filename, 'w') as fil:
        for key in config:
            fil.write(config[key]['desc']+'\n')
            fil.write('\t'+config[key]['val']+'\n')


def readconfig(filename):
    """Reads configuration file , filename, and returns a dict of the 
       congig section:item:val"""
    
    config = ConfigObj(filename)
    return config

def update_config(config, amp = 3, phi = 0.15):
    config['amplitude']['val'] = str(amp)
    nfr = (71.00/15.00) * phi
    config['#rods']['val'] = str(nfr)
    return config




if __name__ == '__main__':
    pass